{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1f8685f2-ec60-460e-9089-0760551d603b",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "from langchain.output_parsers import CommaSeparatedListOutputParser\n",
    "from langchain.prompts import ChatPromptTemplate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "82f1f152-4203-453f-9837-f0e71135f193",
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"{parser_instructions}\"),\n",
    "    (\"human\", \"列出5个{subject}色系的十六进制颜色码。\")\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a478e1c1-a61c-425b-8127-01c969504759",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your response should be a list of comma separated values, eg: `foo, bar, baz`\n"
     ]
    }
   ],
   "source": [
    "output_parser = CommaSeparatedListOutputParser()\n",
    "parser_instructions = output_parser.get_format_instructions()\n",
    "print(parser_instructions)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "734cde9b-7ccc-4b76-b6d8-68f29c3194da",
   "metadata": {},
   "outputs": [],
   "source": [
    "final_prompt = prompt.invoke({\"subject\": \"莫兰迪\", \"parser_instructions\": parser_instructions})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3431d3d2-d0ae-447d-934c-e5965a390145",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#FF6B6B, #FF8E53, #FFC93C, #FFD166, #99E2D0\n"
     ]
    }
   ],
   "source": [
    "model = ChatOpenAI(model=\"gpt-3.5-turbo\")\n",
    "response = model.invoke(final_prompt)\n",
    "print(response.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "0f71896d-af52-4a85-9315-556d9b3b518d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['#FF6B6B', '#FF8E53', '#FFC93C', '#FFD166', '#99E2D0']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_parser.invoke(response)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9da1acda-32f4-42ae-88ef-f9f38de5cab6",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
